﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace EFS.RC.Presences.Controls.Lync.Presences
{

    class HuntGroup
    {
        private Guid _HuntGroupID;
        private string _HuntGroupName;
        private string _HuntGroupDescription;
        private string _PrimaryURI;
        
        public string HuntGroupName
        {
            get { return _HuntGroupName; }
			set { _HuntGroupName = value; }
        }
        public string HuntGroupDescription
        {
            get { return _HuntGroupDescription; }
			set { _HuntGroupDescription = value; }
        }
        public string PrimaryURI
        {
            get { return _PrimaryURI; }
			set { _PrimaryURI = value; }
        }

        public List<HuntGroupMember> Members = new List<HuntGroupMember>();

        public void LoadGroup(string HuntGroupName)
        {
            //build the hunt group members
            Members.Clear();

            SqlConnection cnx = new SqlConnection(EFS.RC.Presences.Properties.Settings.Default.LyncRTC);

            string Query = EFS.RC.Presences.Properties.Resources.GetHuntGroupSQL;

            SqlCommand cmd = new SqlCommand(Query, cnx);
            cmd.Parameters.AddWithValue("@ResponseGroupName", HuntGroupName);

			try
			{
				cnx.Open();

				if (cnx.State == ConnectionState.Open)
				{
					SqlDataReader rdr = cmd.ExecuteReader();
					while (rdr.Read())
					{
						_HuntGroupID = rdr.IsDBNull(0) ? new Guid() : rdr.GetGuid(0);
						_HuntGroupName = rdr.IsDBNull(1) ? "" : rdr.GetString(1);
						_HuntGroupDescription = rdr.IsDBNull(2) ? "" : rdr.GetString(2);
						_PrimaryURI = rdr.IsDBNull(3) ? "" : rdr.GetString(3);

						System.Diagnostics.Debug.WriteLine("user: " + rdr.GetString(5));

						Members.Add(new HuntGroupMember(rdr));
					}

					cnx.Close();
				}
			}
			catch (System.Exception ex)
			{
				EFS.EventLog.Log.LogException(ex);
			}
            finally
            {
                cmd.Dispose();
                cnx.Dispose();
            }
        }

        public void RefreshStatus()
        {
            //refresh the logged in status of the hunt group members
            SqlConnection cnx = new SqlConnection(EFS.RC.Presences.Properties.Settings.Default.LyncRTC);

            string Query = EFS.RC.Presences.Properties.Resources.LogonStatusSQL;

            SqlCommand cmd = new SqlCommand(Query, cnx);
            cmd.Parameters.AddWithValue("@GroupId", _HuntGroupID);

            try
            {
                cnx.Open();

                if (cnx.State == ConnectionState.Open)
                {
                    SqlDataReader rdr = cmd.ExecuteReader();
                    while (rdr.Read())
                    {
                        Guid AgentID = rdr.IsDBNull(0) ? new Guid() : rdr.GetGuid(0);
                        foreach (HuntGroupMember member in Members)
                        {
                            if (member.AgentID == AgentID)
                            {
                                member.LoggedIn = rdr.IsDBNull(2) ? true : (rdr.GetByte(2) == 1);
                                break;
                            }
                        }
                    }

                    cnx.Close();
                }
            }
            finally
            {
                cmd.Dispose();
                cnx.Dispose();
            }
        }
        
        public HuntGroup(string HuntGroupName)
        {
            LoadGroup(HuntGroupName);
        }
    }
    
    class HuntGroupMember
    {

        private Guid _AgentID;
        private string _AgentDisplayName;
        private string _AgentSIP;
        private bool _LoggedIn;

        public Guid AgentID
        {
            get { return _AgentID; }
			set { _AgentID = value;  }
        }
        public string AgentDisplayName
        {
            get { return _AgentDisplayName; }
			set { _AgentDisplayName = value; }
        }
        public string AgentSIP
        {
            get { return _AgentSIP; }
			set { _AgentSIP = value; }
        }

        public bool LoggedIn
        {
            get { return _LoggedIn; }
			set { _LoggedIn = value; }
        }

        public HuntGroupMember()
        {
        }

        public HuntGroupMember(Guid AAgentID, string AAgentDisplayName, string AAgentSIP, bool ALoggedIn)
        {
            _AgentID = AAgentID;
            _AgentSIP = AAgentSIP;
            _AgentDisplayName = AAgentDisplayName;
            _LoggedIn = ALoggedIn;
        }

        public HuntGroupMember(SqlDataReader rdr)
        {
            _AgentID = rdr.IsDBNull(4) ? new Guid() : rdr.GetGuid(4);
            _AgentDisplayName = rdr.IsDBNull(5) ? "" : rdr.GetString(5);
            _AgentSIP = rdr.IsDBNull(6) ? "" : rdr.GetString(6);
            _LoggedIn = rdr.IsDBNull(8) ? true : (rdr.GetInt32(8) == 1);
        }
    }
}
